package com.inei.reportedeverificacion.view;

import java.util.ArrayList;
import java.util.List;
import com.inei.reportedeverificacion.R;
import com.inei.reportedeverificacion.controller.ReporteCap12Controller;
import com.inei.reportedeverificacion.controller.ReporteController;
import com.inei.reportedeverificacion.model.entity.Reporte;
import com.inei.reportedeverificacion.model.entity.ReporteCap12;
import com.inei.reportedeverificacion.model.entity.ReporteCap12Tmp;
import com.inei.reportedeverificacion.model.entity.ReporteTmp;
import com.inei.reportedeverificacion.tools.HolderCap12;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Fragment_Seccion_4 extends Activity {

	// componentes
	private TextView tvC13_1;
	private Spinner spC13_1;
	private LinearLayout llC12;
	public List<Spinner> spinnerCap12_1, spinnerCap12_2, spinnerCap12_3,
			spinnerCap12_4, spinnerCap12_5, spinnerCap12_6;

	// variables
	private ReporteCap12Controller con12;
	private List<ReporteCap12> cap12;
	private List<ReporteCap12Tmp> cap12Tmp;
	private ReporteController con;
	private Reporte rep;
	private ReporteTmp repTmp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_seccion_4);

		con = new ReporteController(getApplicationContext());
		con12 = new ReporteCap12Controller(getApplicationContext());

		IniciarComponentes();

		CargarDatos();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_frag4, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.mnGuardar:
			GuardarDatos();
			break;
		case R.id.mnSalir:
			finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		Intent ir_frag3 = new Intent(this, Fragment_Seccion_3.class);
		ir_frag3.putExtras(getIntent().getExtras());
		startActivity(ir_frag3);
		finish();

		super.onBackPressed();
	}

	private void IniciarComponentes() {
		tvC13_1 = (TextView) findViewById(R.id.tvC13_1);

		spC13_1 = (Spinner) findViewById(R.id.spC13_1);

		llC12 = (LinearLayout) findViewById(R.id.llC12);
	}

	private void CargarDatos() {
		repTmp = new ReporteTmp();
		repTmp.setCLAVEVIVHOGAR(getIntent().getExtras().getString(
				"CLAVEVIVHOGAR"));
		repTmp = con.ReporteTmpBuscar(repTmp);

		if (repTmp != null) {
			if (repTmp.getC1P340_INF() != null) {
				tvC13_1.setText(repTmp.getC1P340_INF());
			}
		}

		rep = new Reporte();
		rep.setCLAVEVIVHOGAR(getIntent().getExtras().getString("CLAVEVIVHOGAR"));
		rep = con.ReporteBuscar(rep);

		if (rep != null) {
			if (rep.getC1P340_INF() != 0) {
				spC13_1.setSelection(rep.getC1P340_INF());
			}
		}

		cap12Tmp = con12.ReporteCap12TmpLeerXHOGAR(getIntent().getExtras()
				.getString("CLAVEVIVHOGAR"));

		cap12 = con12.ReporteCap12LeerXHOGAR(getIntent().getExtras().getString(
				"CLAVEVIVHOGAR"));

		if (cap12Tmp != null) {
			CargarCap12();
		}
	}

	private void CargarCap12() {
		spinnerCap12_1 = new ArrayList<Spinner>();
		spinnerCap12_2 = new ArrayList<Spinner>();
		spinnerCap12_3 = new ArrayList<Spinner>();
		spinnerCap12_4 = new ArrayList<Spinner>();
		spinnerCap12_5 = new ArrayList<Spinner>();
		spinnerCap12_6 = new ArrayList<Spinner>();
		if (llC12 != null) {
			llC12.removeAllViews();
		}
		for (int i = 0; i < cap12Tmp.size(); i++) {
			View item = null;
			LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			item = inflater.inflate(R.layout.cap12_item, null);
			ReporteCap12Tmp obj = cap12Tmp.get(i);
			HolderCap12 holder = new HolderCap12();
			holder.tvC12 = (TextView) item.findViewById(R.id.tvC12);
			holder.tvC12_1 = (TextView) item.findViewById(R.id.tvC12_1);
			holder.tvC12_2 = (TextView) item.findViewById(R.id.tvC12_2);
			holder.tvC12_3 = (TextView) item.findViewById(R.id.tvC12_3);
			holder.tvC12_4 = (TextView) item.findViewById(R.id.tvC12_4);
			holder.tvC12_5 = (TextView) item.findViewById(R.id.tvC12_5);
			holder.tvC12_6 = (TextView) item.findViewById(R.id.tvC12_6);
			holder.spC12_1 = (Spinner) item.findViewById(R.id.spC12_1);
			holder.spC12_2 = (Spinner) item.findViewById(R.id.spC12_2);
			holder.spC12_3 = (Spinner) item.findViewById(R.id.spC12_3);
			holder.spC12_4 = (Spinner) item.findViewById(R.id.spC12_4);
			holder.spC12_5 = (Spinner) item.findViewById(R.id.spC12_5);
			holder.spC12_6 = (Spinner) item.findViewById(R.id.spC12_6);

			holder.tvC12
					.setText((obj.getC1P21A() + " " + obj.getC1P21B() + " " + obj
							.getC1P21C()));
			holder.tvC12_1.setText(obj.getT_05_DIG_CAP12_2() + "");
			holder.tvC12_2.setText(obj.getT_05_DIG_CAP12_4() + "");
			holder.tvC12_3.setText(obj.getT_05_DIG_CAP12_5() + "");
			holder.tvC12_4.setText(obj.getT_05_DIG_CAP12_7() + "");
			holder.tvC12_5.setText(obj.getT_05_DIG_CAP12_8() + "");
			holder.tvC12_6.setText(obj.getT_05_DIG_CAP12_9() + "");

			if (cap12 != null) {
				if (cap12.get(i).getT_05_DIG_CAP12_2() != 0) {
					holder.spC12_1.setSelection(cap12.get(i)
							.getT_05_DIG_CAP12_2());
				}
				if (cap12.get(i).getT_05_DIG_CAP12_4() != 0) {
					holder.spC12_2.setSelection(cap12.get(i)
							.getT_05_DIG_CAP12_4());
				}
				if (cap12.get(i).getT_05_DIG_CAP12_5() != 0) {
					holder.spC12_3.setSelection(cap12.get(i)
							.getT_05_DIG_CAP12_5());
				}
				if (cap12.get(i).getT_05_DIG_CAP12_7() != 0) {
					holder.spC12_4.setSelection(cap12.get(i)
							.getT_05_DIG_CAP12_7());
				}
				if (cap12.get(i).getT_05_DIG_CAP12_8() != 0) {
					holder.spC12_5.setSelection(cap12.get(i)
							.getT_05_DIG_CAP12_8());
				}
				if (cap12.get(i).getT_05_DIG_CAP12_9() != 0) {
					holder.spC12_6.setSelection(cap12.get(i)
							.getT_05_DIG_CAP12_9());
				}
			}

			spinnerCap12_1.add(holder.spC12_1);
			spinnerCap12_2.add(holder.spC12_2);
			spinnerCap12_3.add(holder.spC12_3);
			spinnerCap12_4.add(holder.spC12_4);
			spinnerCap12_5.add(holder.spC12_5);
			spinnerCap12_6.add(holder.spC12_6);
			llC12.addView(item);
		}
	}

	public void GuardarDatos() {
		rep.setC1P340_INF(spC13_1.getSelectedItemPosition());

		con.ReporteModificar(rep);

		int n = 0;
		if (cap12 == null) {
			cap12 = new ArrayList<ReporteCap12>();
			for (int i = 0; i < cap12Tmp.size(); i++) {
				ReporteCap12 r = new ReporteCap12();
				r.setCLAVEVIVHOGAR(getIntent().getExtras().getString(
						"CLAVEVIVHOGAR"));
				r.setNRO_ORDEN(cap12Tmp.get(i).getNRO_ORDEN());
				cap12.add(r);
			}
			n = 1;
		}
		for (int i = 0; i < cap12Tmp.size(); i++) {
			cap12.get(i).setT_05_DIG_CAP12_2(
					spinnerCap12_1.get(i).getSelectedItemPosition());
			cap12.get(i).setT_05_DIG_CAP12_4(
					spinnerCap12_2.get(i).getSelectedItemPosition());
			cap12.get(i).setT_05_DIG_CAP12_5(
					spinnerCap12_3.get(i).getSelectedItemPosition());
			cap12.get(i).setT_05_DIG_CAP12_7(
					spinnerCap12_4.get(i).getSelectedItemPosition());
			cap12.get(i).setT_05_DIG_CAP12_8(
					spinnerCap12_5.get(i).getSelectedItemPosition());
			cap12.get(i).setT_05_DIG_CAP12_9(
					spinnerCap12_6.get(i).getSelectedItemPosition());
			if (n == 1) {
				if (con12.ReporteCap12Insertar(cap12.get(i)) > 0) {
					Intent ir_vivienda = new Intent(this,
							Vivienda_Activity.class);
					Bundle datos = getIntent().getExtras();
					ir_vivienda.putExtras(datos);
					startActivity(ir_vivienda);

					Toast.makeText(this, "Se guardo con exito",
							Toast.LENGTH_SHORT).show();

					finish();
				} else {
					Toast.makeText(this, "Error al guardar", Toast.LENGTH_SHORT)
							.show();
				}
			} else {
				if (con12.ReporteCap12Modificar(cap12.get(i)) > 0) {
					Intent ir_vivienda = new Intent(this,
							Vivienda_Activity.class);
					Bundle datos = getIntent().getExtras();
					ir_vivienda.putExtras(datos);
					startActivity(ir_vivienda);

					Toast.makeText(this, "Se actualizo con exito",
							Toast.LENGTH_SHORT).show();

					finish();
				} else {
					Toast.makeText(this, "Error al actualizar",
							Toast.LENGTH_SHORT).show();
				}
			}
		}
	}

	private String tabla01(int pos) {
		return getResources().getStringArray(R.array.tabla01)[pos];
	}

	private String tabla02(int pos) {
		return getResources().getStringArray(R.array.tabla02)[pos];
	}

	private String tabla03(int pos) {
		return getResources().getStringArray(R.array.tabla03)[pos];
	}

	private String rptaSiNo(int pos) {
		return getResources().getStringArray(R.array.rptaSiNo)[pos];
	}
}
